package com.szpx.controller.XWYController;

import com.szpx.entity.XWY.XWYDenglu;
import com.szpx.service.XWYDengluService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * XWY登录控制器
 */
@RestController
@RequestMapping("/api/xwy")
public class XWYDengluController {

    @Autowired
    private XWYDengluService xwyDengluService;

    /**
     * 用户登录
     * @param account 账号（手机号或用户名）
     * @param password 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(
            @RequestParam String account,
            @RequestParam String password) {
        
        Map<String, Object> response = new HashMap<>();
        XWYDenglu user = xwyDengluService.login(account, password);
        
        if (user != null) {
            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", user);
        } else {
            response.put("code", 401);
            response.put("message", "账号或密码错误");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 带角色的登录（使用手机号）
     * @param phone 手机号
     * @param password 密码
     * @param role 角色
     * @return 登录结果
     */
    @PostMapping("/login/role")
    public ResponseEntity<Map<String, Object>> loginWithRole(
            @RequestParam String phone,
            @RequestParam String password,
            @RequestParam String role) {
        
        Map<String, Object> response = new HashMap<>();
        XWYDenglu user = xwyDengluService.loginWithRole(phone, password, role);
        
        if (user != null) {
            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", user);
        } else {
            response.put("code", 401);
            response.put("message", "账号、密码或角色错误");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 获取所有用户列表
     * @return 用户列表
     */
    @GetMapping("/users")
    public ResponseEntity<Map<String, Object>> getAllUsers() {
        Map<String, Object> response = new HashMap<>();
        List<XWYDenglu> users = xwyDengluService.selectAlldenglu();
        
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", users);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 检查用户名是否存在
     * @param username 用户名
     * @return 检查结果
     */
    @GetMapping("/check-username")
    public ResponseEntity<Map<String, Object>> checkUsername(@RequestParam String username) {
        Map<String, Object> response = new HashMap<>();
        boolean exists = xwyDengluService.checkUsername(username);
        
        response.put("code", 200);
        response.put("message", "检查完成");
        response.put("exists", exists);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 用户注册
     * @param user 用户信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody XWYDenglu user) {
        Map<String, Object> response = new HashMap<>();
        
        // 参数验证
        if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
            response.put("code", 400);
            response.put("message", "用户名不能为空");
            return ResponseEntity.ok(response);
        }
        
        if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
            response.put("code", 400);
            response.put("message", "密码不能为空");
            return ResponseEntity.ok(response);
        }
        
        if (user.getPhone() == null || !user.getPhone().matches("^1[3-9]\\d{9}$")) {
            response.put("code", 400);
            response.put("message", "手机号格式不正确");
            return ResponseEntity.ok(response);
        }
        
        // 调用注册服务
        int result = xwyDengluService.register(user);
        
        if (result > 0) {
            response.put("code", 200);
            response.put("message", "注册成功");
            response.put("data", user);
        } else {
            response.put("code", 400);
            response.put("message", "注册失败，用户名或手机号已存在");
        }
        
        return ResponseEntity.ok(response);
    }
}